package com.hqyj.controller;

import com.hqyj.pojo.Goods;
import com.hqyj.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @RequestMapping("showGoods")
    public String showGoods(HttpServletRequest request){
        String name = request.getParameter("userName");
        System.out.println("================当前的用户名为："+name+"==========");
        request.setAttribute("userName",name);
        return "goods/showList";
    }

    @RequestMapping("detailsGoods")
    public String detailsGoods(HttpServletRequest request){

        System.out.println("==============="+request.getParameter("gId"));
        return "goods/details";
    }
    @RequestMapping("requestGoodsList")
    @ResponseBody
    public HashMap<String,Object> requestGoodsList(Goods goods, HttpServletRequest request){

        int i = Integer.parseInt(request.getParameter("number"));
        request.setAttribute("i",goodsService.showGoodsList(goods,i));
        System.out.println(i);
        return goodsService.showGoodsList(goods,i);
    }
    //表单文件上传
    @RequestMapping("/selectGoodsPhotos")
    @ResponseBody
    public HashMap<String,Object> selectGoodsPhotos(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        Goods goods = new Goods();
        System.out.println(request.getParameter("g_prices"));
        goods.setgUserId(Integer.parseInt( request.getParameter("userId")));
        goods.setgName(request.getParameter("g_name"));
        goods.setgPrice(Integer.parseInt(request.getParameter("g_prices")));
        goods.setgNumber(Integer.parseInt( request.getParameter("g_number")));
        String gType = null ;
        if (Integer.parseInt (request.getParameter("selectShopTpye"))==1){
            gType = "书籍类";
        }else if (Integer.parseInt (request.getParameter("selectShopTpye"))==2){
            gType = "生活用品";
        }else{
            gType = "体育用品";
        }
        goods.setgType(gType);

        System.out.println(goods);
        return goodsService.selectGoodsPhotos(goods,request,file);
    }

    @RequestMapping("buyGoodsList")
    @ResponseBody
    public HashMap<String,Object> buyGoodsList(Goods goods, HttpServletRequest request){

        String userName = request.getParameter("userName");
        return goodsService.insertIntoNewBuy(goods,userName);

    }
    @RequestMapping("orderList")
    public String orderList(Goods goods, HttpServletRequest request){
        String userName = request.getParameter("userName");
        request.setAttribute("userName",userName);
        return "admin/orderList";
    }
    @RequestMapping("orderListAjax")
    @ResponseBody
    public HashMap<String,Object> orderListAjax(Goods goods, HttpServletRequest request){

        String userName = request.getParameter("userName");
        return goodsService.selectHavedBuyGoods(goods,userName);

    }
}
